﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using XiDainChangAn.OA.DALFactory;
using XiDainChangAn.OA.IDAL;

namespace XiDainChangAn.OA.BLL
{
    /// <summary>
    /// 父类要逼迫自己给父类的一个属性赋值
    /// 赋值的操作必须要在父类的方法调用之前执行
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseService<T> where T : class, new()//抽象类
    {
        public IBaseDal<T> CurrentDal { get; set; }

        public abstract void GetDal();//抽象方法：要求子类必须实现
        public BaseService()//基类的抽象方法
        {
            GetDal();//抽象方法
        }

        #region MyRegion
        public IDbSession dbSession
        {
            get
            {
                return DbSessionFactory.GetCurrentDbSession();
            }
        } 
        #endregion

        #region 01，增+T Add(T entity)
        /// <summary>
        /// 增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(T entity)
        {
            CurrentDal.Add(entity);
            return dbSession.SaveChange() > 0;
        } 
        #endregion

        #region 02，改+bool Update(T entity)
        // <summary>
        /// 改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpDate(T entity)
        {
            CurrentDal.UpDate(entity);
            return dbSession.SaveChange() > 0;
        } 
        #endregion

        #region 03，删+bool Delete(T entity)
        /// <summary>
        /// 删
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(T entity)
        {
            CurrentDal.Delete(entity);
            return dbSession.SaveChange() > 0;
        }
        #endregion

        public IQueryable<T> GetEntitys(Expression<Func<T, bool>> whereLamdba)
        {
            return CurrentDal.GetEntitys(whereLamdba);
        }

        public T GetById(int id)
        {
            return CurrentDal.GetById(id);
        }

        public IQueryable<T> GetPageEntitys<S>(int pageSize,int pageIndex,out int total, bool isAsc, Expression<Func<T, bool>> whereLamdba,Expression<Func<T,S>> orderByLambda)
        {
            return CurrentDal.GetPageEntitys(pageSize,pageIndex,out total,isAsc,whereLamdba,orderByLambda);
        }
    }
}
